const mqtt = require('aliyun-iot-mqtt');
//设备身份信息
var options = {
  productKey: "k29v6SCxLPP",
  deviceName: "sh_01",
  deviceSecret: "09c1f71e72c11efee6ce7bcac0161f76",
  host: "iot-31z00j1turadv3n.mqtt.iothub.aliyuncs.com"
};

//建立MQTT连接
const client = mqtt.getAliyunIotMqttClient(options);
//订阅云端指令Topic
client.subscribe(`/${options.productKey}/${options.deviceName}/user/get`)
client.subscribe(`/sys/${options.productKey}/${options.deviceName}/thing/event/property/post_reply`)
client.on('message', function (topic, message) {
  console.log("topic " + topic)
  console.log("message " + message)
})

setInterval(function () {
  //定时上报数据
  client.publish(`/sys/${options.productKey}/${options.deviceName}/thing/event/property/post`, getPostData(), { qos: 0 });
}, 5 * 1000);




function getPostData() {
	
	//体温 取值范围：0 ~ 100
	var  bodyTemperature = Math.floor((Math.random() *5) +34);
	//血压 取值范围：-40.0 ~ 55.0
	var bloodPressure = Math.floor((Math.random() * 20) + 130);
	//心率
	var heartRate = Math.floor((Math.random() * 20) + 60);
  //正常
	// if(indoorTemperature > 55){
	// 	smokeSensorState = 1;//异常
	// }
	
	// if(smokeSensorState === 1 ){
	// 	batteryLevel--;
	// }

	
  const payloadJson = {
    id: Date.now(),
    version: "1.0",
    params: {
    BodyTemperature: bodyTemperature,
    BloodPressure: bloodPressure,
	  HeartRate: heartRate
    },
    method: "thing.event.property.post"

  }
  console.log("payloadJson " + JSON.stringify(payloadJson))
  return JSON.stringify(payloadJson);
}